package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import school.domain.Route;
import school.mapper.RouteMapper;
import school.support.BaseController;
@Controller
@RequestMapping("route")
public class RouteController extends BaseController {

    @Autowired
    private RouteMapper routeMapper;

    /**
     * 发表
     *
     * @return
     */
    @GetMapping("send")
    public String send() {
        if (isAdmin()) return "send-route";
        return "redirect:/login.do";
    }

    /**
     * 发表
     *
     * @return
     */
    @PostMapping("send")
    public String send(Route route) {
        if (isAdmin()) {
            //route.setUserid(loginUser().getId());
            routeMapper.insert(route);
            return "redirect:/index.do";
        }
        return "redirect:/login.do";
    }

    @RequestMapping("bm")
    public String bm(Integer routeid, RedirectAttributes model){
        if(isLogin()){
            Route route = routeMapper.selectById(routeid);
            route.setNumyes(route.getNumyes() + 1);
            routeMapper.update(route);
            routeMapper.insertOrder(loginUser().getId(),routeid);
            model.addAttribute("msg","报名成功");
            return refresh();
        }
        return "redirect:/login.do";
    }

}
